package com.yk._19死锁;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;


/**
 * jps -l 查看可能出现死锁的进程号
 * jstack 进程号 查看堆栈信息 发现死锁
 *
 */
public class _01DeadLock {
    public static void main(String[] args) {

        Resource abresource = new Resource("A", "B");
        Resource baresource = new Resource("B", "A");

        new Thread(abresource,"A").start();
        new Thread(baresource, "B").start();
    }


}

class Resource implements Runnable {

    private Object A;
    private Object B;

    public Resource(Object a, Object b) {
        A = a;
        B = b;
    }

    @SneakyThrows
    @Override
    public void run() {
        synchronized (A) {
            //暂停休眠,一定死锁
            TimeUnit.SECONDS.sleep(2);
            synchronized (B) {
                System.out.println(Thread.currentThread().getName() + "程序憋死了,我不能输出!!!!!");
            }
        }

    }
}